package com.bicon.botu.mall.goods.rpc.service;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.bicon.botu.core.base.common.BusinessException;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.goods.commons.GoodsConstants;
import com.bicon.botu.mall.goods.dto.OrderVerifyDto;
import com.bicon.botu.mall.goods.dto.ProductRepParamDto;
import com.bicon.botu.mall.goods.entity.ProductRepertory;
import com.bicon.botu.mall.goods.service.IProductRepertoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 库存对外提供RPC
 */
@Service
public class ProductRepertoryRpc{

    @Autowired
    private IProductRepertoryService iProductRepertoryService;

    @RequestMapping(value = "/getProductRepInfo", method = RequestMethod.POST)
    public String getProductRepInfo(@RequestBody List<ProductRepParamDto> productRepParamDtos) {
        for (ProductRepParamDto paramDto : productRepParamDtos){
            ProductRepertory entity=iProductRepertoryService.selectById(paramDto.getProductRepertoryId());
            if(entity != null){
                paramDto.setPrice(entity.getPrice());
            }
        }
        return JSONArray.toJSONString(productRepParamDtos);
    }

    /**
     * 根据商品库存ID查询商品库存详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/selectProductById", method = RequestMethod.POST)
    public String selectProductById(@RequestParam String id){
        return JSONObject.toJSONString(iProductRepertoryService.selectById(id));
    }


    /**
     * 根据商品库存ID回退商品库存数量
     * @param id
     * @return
     */
    @RequestMapping(value = "/rollbackById", method = RequestMethod.POST)
    public BaseResult rollbackById(@RequestParam Integer num, @RequestParam String id){
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, iProductRepertoryService.updateNumberById(GoodsConstants.NUMBER_1, num, id));
    }

    /**
     * 回退库存的数量
     * @param verifyDtoListJson
     * @return
     */
    @RequestMapping(value = "/rollbackNum", method = RequestMethod.POST)
    public BaseResult rollbackNum(@RequestParam String verifyDtoListJson) {
        if (StringUtils.isEmpty(verifyDtoListJson)) {
            throw new BusinessException("商品信息不能为空");
        }
        Type type = new TypeReference<List<OrderVerifyDto>>(){}.getType();
        List<OrderVerifyDto> orderVerifyDtoList = JSONObject.parseObject(verifyDtoListJson,type);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, iProductRepertoryService.updateNumber(orderVerifyDtoList));
    }

    /**
     * 回退库存的数量
     * @param verifyDtoListJson 库存id
     * @return
     */
    @RequestMapping(value = "/addSales", method = RequestMethod.POST)
    public BaseResult addSales(@RequestParam String verifyDtoListJson) {
        if (StringUtils.isEmpty(verifyDtoListJson)) {
            throw new BusinessException("商品信息不能为空");
        }
        Type type = new TypeReference<List<OrderVerifyDto>>(){}.getType();
        List<OrderVerifyDto> orderVerifyDtoList = JSONObject.parseObject(verifyDtoListJson,type);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, iProductRepertoryService.addSalesByList(orderVerifyDtoList));
    }

    /**
     * 根据商品库存ID回退商品库存数量
     * @param id
     * @return
     */
    @RequestMapping(value = "/addSalesById", method = RequestMethod.POST)
    public BaseResult addSalesById(@RequestParam Integer num, @RequestParam String id){
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, iProductRepertoryService.addSalesById(num, id));
    }


}
